package cn.huanglei936.essay.thymeleaf.api.ucenter;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * ================================================
 * Discuz! Ucenter API for JAVA
 * ================================================
 * 此类用来同步UC Server发出的操作指令
 * 可以根据业务需要添加相应的执行代码
 * <p>
 * 作者：梁平
 * 创建时间：2009-2-20
 */
public class UC extends HttpServlet {

    private static final long serialVersionUID = -7377364931916922413L;

    public static boolean IN_DISCUZ = true;
    //note UCenter 版本标识
    public static String UC_CLIENT_VERSION = "1.5.0";
    public static String UC_CLIENT_RELEASE = "20081031";

    /**
     * note 用户删除 API 接口开关
     */
    public static boolean API_DELETEUSER = true;
    /**
     * note 用户改名 API 接口开关
     */
    public static boolean API_RENAMEUSER = true;
    /**
     * note 获取标签 API 接口开关
     */
    public static boolean API_GETTAG = true;
    /**
     * note 同步登录 API 接口开关
     */
    public static boolean API_SYNLOGIN = true;
    /**
     * note 同步登出 API 接口开关
     */
    public static boolean API_SYNLOGOUT = true;
    /**
     * note 更改用户密码 开关
     */
    public static boolean API_UPDATEPW = true;
    /**
     * note 更新关键字列表 开关
     */
    public static boolean API_UPDATEBADWORDS = true;
    /**
     * note 更新域名解析缓存 开关
     */
    public static boolean API_UPDATEHOSTS = true;
    /**
     * note 更新应用列表 开关
     */
    public static boolean API_UPDATEAPPS = true;
    /**
     * note 更新客户端缓存 开关
     */
    public static boolean API_UPDATECLIENT = true;
    /**
     * note 更新用户积分 开关
     */
    public static boolean API_UPDATECREDIT = true;
    /**
     * note 向 UCenter 提供积分设置 开关
     */
    public static boolean API_GETCREDITSETTINGS = true;
    /**
     * note 获取用户的某项积分 开关
     */
    public static boolean API_GETCREDIT = true;
    /**
     * note 更新应用积分设置 开关
     */
    public static boolean API_UPDATECREDITSETTINGS = true;

    public static String API_RETURN_SUCCEED = "1";
    public static String API_RETURN_FAILED = "-1";
    public static String API_RETURN_FORBIDDEN = "-2";

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println(" >>>>> UC doGet ");
        String result = doAnswer(request, response);
        response.getWriter().print(result);
    }

    /**
     * 执行具体的Action
     * 所有服务器发出的参数均可通过$get来获得。
     * 注意： request本身是不能得到参数值的。
     *
     * @param request
     * @param response
     * @return 操作状态或操作结果
     */
    private String doAnswer(HttpServletRequest request, HttpServletResponse response) {
        //处理
        String $code = request.getParameter("code");
        if ($code == null) {
            return API_RETURN_FAILED;
        }

        Map<String, String> $get = new HashMap<String, String>();
        $code = new Client().uc_authcode($code, "DECODE");
        parse_str($code, $get);

        if ($get.isEmpty()) {
            return "Invalid Request";
        }
        if (time() - tolong($get.get("time")) > 3600) {
            return "Authracation has expiried";
        }

        String $action = $get.get("action");
        if ($action == null) {
            return API_RETURN_FAILED;
        }

        if ("test".equals($action)) {

            return API_RETURN_SUCCEED;

        }
        else if ("deleteuser".equals($action)) {

            System.out.println(" >>>>> UC deleteuser ");
            return API_RETURN_SUCCEED;

        }
        else if ("renameuser".equals($action)) {

            System.out.println(" >>>>> UC renameuser ");
            return API_RETURN_SUCCEED;

        }
        else if ("gettag".equals($action)) {

            System.out.println(" >>>>> UC gettag ");

            if (!API_GETTAG) {
                return API_RETURN_FORBIDDEN;
            }

            //同步代码

            return API_RETURN_SUCCEED;


        }
        else if ("synlogin".equals($action)) {

            System.out.println(" >>>>> UC synlogin ");

            if (!API_SYNLOGIN) {
                return (API_RETURN_FORBIDDEN);
            }

            //note 同步登录 API 接口
            //obclean();
            response.addHeader("P3P", "CP=\"CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR\"");

            int $cookietime = 31536000;

            Cookie user = new Cookie("loginuser", $get.get("username"));
            user.setMaxAge($cookietime);
            response.addCookie(user);

        }
        else if ("synlogout".equals($action)) {

            System.out.println(" >>>>> UC synlogout ");

            if (!API_SYNLOGOUT) {
                return (API_RETURN_FORBIDDEN);
            }

            //note 同步登出 API 接口
            //obclean();
            response.addHeader("P3P", " CP=\"CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR\"");

            //clearcookie();
            Cookie user = new Cookie("loginuser", "");
            user.setMaxAge(0);
            response.addCookie(user);

        }
        else if ("updateclient".equals($action)) {

            System.out.println(" >>>>> UC updateclient ");

            if (!API_UPDATECLIENT) {
                return API_RETURN_FORBIDDEN;
            }


            //同步代码

            return API_RETURN_SUCCEED;

        }
        else if ("updatepw".equals($action)) {

            System.out.println(" >>>>> UC updatepw ");

            if (!API_UPDATEPW) {
                return API_RETURN_FORBIDDEN;
            }

            //同步代码

            return API_RETURN_SUCCEED;

        }
        else if ("updatebadwords".equals($action)) {

            System.out.println(" >>>>> UC updatebadwords ");

            if (!API_UPDATEBADWORDS) {
                return API_RETURN_FORBIDDEN;
            }

            //同步代码

            return API_RETURN_SUCCEED;

        }
        else if ("updatehosts".equals($action)) {

            System.out.println(" >>>>> UC updatehosts ");

            if (!API_UPDATEHOSTS) {
                return API_RETURN_FORBIDDEN;
            }


            return API_RETURN_SUCCEED;

        }
        else if ("updateapps".equals($action)) {

            System.out.println(" >>>>> UC updateapps ");

            if (!API_UPDATEAPPS) {
                return API_RETURN_FORBIDDEN;
            }


            return API_RETURN_SUCCEED;

        }
        else if ("updatecredit".equals($action)) {

            System.out.println(" >>>>> UC updatecredit ");

            //if(!UPDATECREDIT ) return API_RETURN_FORBIDDEN;

            return API_RETURN_SUCCEED;

        }
        else if ("getcreditsettings".equals($action)) {

            System.out.println(" >>>>> UC getcreditsettings ");

            //if(!GETCREDITSETTINGS ) return API_RETURN_FORBIDDEN;

            return "";//积分值

        }
        else if ("updatecreditsettings".equals($action)) {

            System.out.println(" >>>>> UC updatecreditsettings ");


            if (!API_UPDATECREDITSETTINGS) {
                return API_RETURN_FORBIDDEN;
            }


            //同步代码

            return API_RETURN_SUCCEED;

        }
        else {

            System.out.println(" >>>>> UC 。。。。。。。。。。。。。。。 ");

            return (API_RETURN_FORBIDDEN);

        }
        return "";
    }

    private void parse_str(String str, Map<String, String> sets) {
        if (str == null || str.length() < 1) {
            return;
        }
        String[] ps = str.split("&");
        for (int i = 0; i < ps.length; i++) {
            String[] items = ps[i].split("=");
            if (items.length == 2) {
                sets.put(items[0], items[1]);
            }
            else if (items.length == 1) {
                sets.put(items[0], "");
            }
        }
    }

    protected long time() {
        return System.currentTimeMillis() / 1000;
    }

    private static long tolong(Object s) {
        if (s != null) {
            String ss = s.toString().trim();
            if (ss.length() == 0) {
                return 0L;
            }
            else {
                return Long.parseLong(ss);
            }
        }
        else {
            return 0L;
        }
    }

}
